( function ( D ) 
{
    D.updateImages = function ( line ) 
    {
        var firstVisibleImage = line.firstVisibleImage,
            nbImages = line.nbImages,
            visibleImages = [];
    
        var quotients = function ( ) 
        {
            var images    = line.grid.getElementsByTagName( "img" ),
                quotients = 0;
            
            for( var i = 0 ; i < images.length ; i += 1 ) 
            {
                quotients += images[ i ].offsetWidth / 
                             images[ i ].offsetHeight;
            }
            
            return quotients;
        }
    
        var newHeight = function ( ) 
        {
            // the neavigator scrollbar takes ~20px
            var scrollBarOffset = 20; 

            return Math.floor (
                ( line.grid.offsetWidth - 
                  nbImages * 20 - scrollBarOffset ) / 
                quotients()
            );
        }

        var imageLoaded = function ( ) 
        {
            var loadCount = 0;

            return function () 
            {
                loadCount += 1;

                if( loadCount === nbImages ) 
                {
                    var h = newHeight( );

                    // 22px is the height of the numPage divs :
                    line.grid.style.height = h + 22 + "px"; 
                    for( var i = 0 ; i < visibleImages.length ; i += 1 ) 
                    {
                        visibleImages[i].style.height = h + "px";
                    }
                }
            }
        } ( );
    
        var actualNumberOfImages = line.grid.childNodes.length;
    
        for( var i = 0 ; i < actualNumberOfImages ; i += 1 ) 
        {
            line.grid.removeChild(line.grid.childNodes[0]);
        }
    
        if( line.images.length > 0 ) 
        {
            for( var i = firstVisibleImage ; 
                 (i < (firstVisibleImage + nbImages)) && 
                 (i < line.images.length  ) ; 
                 i += 1 ) 
            {
                var li  = document.createElement( "li" );
                var pageTop = document.createElement( "div" );
                var numPage = document.createElement( "div" );
                var links = D.makeControl("Afficher le graphe des objets liés.", "&#x2604;", "pageLinks");
		var editRelations = D.makeControl("Voir/éditer les relations.", "&#x3C1;", "pageLinks");
                var transcription = D.makeControl("Afficher la transcription.", "&#x2710;", "trancription");
                var deletePage = D.makeControl("Supprimer la page.", "suppr", "deletePage");
                var img = document.createElement( "img" );

                pageTop.setAttribute( "class", "pageTop" );

                numPage.setAttribute( "class", "numPage" );
                numPage.innerHTML = ( i + 1 ) + ' / ' + line.images.length;
		
                deletePage.onclick = function( )
                {
                    var j = i;

                    return function( )
                    {
                        if( line.isView )
                        {
                            var images = line.images,
                                newImages = [],
                                offset = 0,
                                pages = "",
                                pagesId = "<" + line.id + "/pages>";

                            for( var i = 0 ; i < images.length ; i += 1 )
                            {
                                if( i === j )
                                {
                                    offset = 1;
                                } 
                                else
                                {
                                    newImages[ i - offset ] = images[ i ];
                                    pages += pagesId + " rdf:_" + ( i + 1 - offset ) + " <" + images[i].id + "> .\n";
                                }
                            }

                            line.setImages( newImages );
                            D.updateImages( line );

                            D.request( "deleteNumPages.txt",
                                {
                                    "collectionId": line.id
                                },
                                function( )
                                {
                                    D.request( "deletePage.txt",
                                        {
                                            "pages": pages,
                                            "timestamp": new Date( ).getTime( ),
                                            "collection": line.id,
                                            "page": images[j].id, 
                                            "user": D.userName( ),
                                        },
                                        null, true 
				    );
                                }, true 
			    );
                        }
                        return false;
                    }
                } ( );

                links.onclick = function( )
                {
                    var j = i;

                    return function( )
                    {
                        var url = self.location.protocol + "//" + self.location.hostname + 
                                  "/graph.html?id=" + line.images[j].id,
                            graphWindow = window.open( url, "graph" );

                        graphWindow.focus( );
                        return false;
                    }
                } ( );

                editRelations.onclick = function( )
                {
                    var j = i;

                    return function( )
                    {
                        var url = self.location.protocol + "//" + self.location.hostname + 
                                  "/pageRelations.html?pageId=" + line.images[j].id,
                            relationsWindow = window.open( url, "pageRelations" );

                        relationsWindow.focus( );
                        return false;
                    }
                } ( );

                transcription.onclick = function( )
                {
                    var j = i;

                    return function( )
                    {
                        var url = self.location.protocol + "//" + self.location.hostname + 
                                  "/transcription.html?pageId=" + line.images[j].id +
                                  "&collectionId=" + line.id,
                            transcriptionWindow = window.open( url, "transcription" );
			D.transcriptionLine = line;

                        transcription.focus( );
                        return false;
                    }
                } ( );

                if( nbImages === 1 )
                {
                    img.setAttribute( "src", line.images[i].big );
                }
                else
                {
                    img.setAttribute( "src", line.images[i].thumb );
                }

                D.request( "getPageRelations.txt",
                    {
                        "page": line.images[i].id
                    },
                    function( )
                    {
			var thisPageLinks = links,
			thisRelations = editRelations;

                        return function( results )
                        {
                            if( results.length > 0 )
                            {
                                thisPageLinks.style.display = "inline";
				thisRelations.style.display = "inline";
                            }
                        }
                    }( ) );

                img.onmousedown = function( )
                {
                    var j = i;

                    return function( e )
                    {
                        D.dragObject = {
			    "type": "page",
                            "i": j,
                            "line": line,
			    "id": line.images[j].id,
			    "thumb": line.images[j].thumb,
			    "big": line.images[j].big
                        };
                        e.stopPropagation( );
                        return false;
                    };

                }( );

                img.onmouseup = function( )
                {
                    var j = i;

                    return function( e )
                    {
                        if( ( ( j != D.dragObject.i ) || ( D.dragObject.line != line ) ) && line.isView )
                        {
                            var operation = document.getElementById( "pagesOperations" ).selectedValue( );

                            if( ( operation === "CopyAfter" ) || ( operation === "CopyBefore" ) )
                            {
                                D.request( "deleteNumPages.txt",
                                    {
                                        "collectionId": line.id
                                    },
                                    function( )
                                    {
                                        var collectionId = line.id,
                                            pagesId = "<" + collectionId + "/pages>",
                                            pages = "",
                                            images = line.images,
                                            newImages = [ ],
                                            newPage = D.dragObject.line.images[ D.dragObject.i ],
					    newPageCollectionId = D.dragObject.line.id,
                                            offset = 0;

                                        if( operation === "CopyAfter" )
                                        {
                                            for( var i = 0 ; i < images.length ; i += 1 )
                                            {
                                                newImages[ i + offset ] = images[ i ];
                                                pages += pagesId + " rdf:_" + ( i + 1 + offset ) + " <" + images[ i ].id + "> .\n";
                                                if( i === j )
                                                {
                                                    newImages[ i + 1 ] = newPage;
                                                    pages += pagesId + " rdf:_" + ( i + 2 ) + " <" + newPage.id + "> .\n" +
                                                             "<" + newPage.id +"> cols:collection <" + 
                                                             collectionId + "> .\n";
                                                    offset = 1;
                                                } 
                                            }
                                        }
                                        else if( operation === "CopyBefore" )
                                        {
                                            for( var i = 0 ; i < images.length ; i += 1 )
                                            {
                                                if( i === j )
                                                {
                                                    newImages[ i ] = newPage;
                                                    pages += pagesId + " rdf:_" + ( i + 1 ) + " <" + newPage.id + "> .\n" +
                                                             "<" + newPage.id +"> cols:collection <" + 
                                                             collectionId + "> .\n";
                                                    offset = 1;
                                                } 
                                                newImages[ i + offset ] = images[ i ];
                                                pages += pagesId + " rdf:_" + ( i + 1 + offset ) + " <" + images[ i ].id + "> .\n";
                                            }
                                        }

                                        line.setImages( newImages ); 
                                        D.updateImages( line );

                                        D.request( "pageOperation.txt",
                                            {
                                                "pages": pages,
                                                "timestamp": new Date( ).getTime( ),
                                                "collection": collectionId,
                                                "page": images[ j ].id, 
                                                "newPage": newPage.id, 
                                                "newPageCollection": newPageCollectionId,
                                                "user": D.userName( ),
                                                "operation": operation
                                            },
                                            null, true 
					);
                                    }, true 
				);
                            }
                        }
                        e.stopPropagation( );
                        return false;
                    };
                }( );

                pageTop.appendChild( numPage );
                pageTop.appendChild( links   );
                pageTop.appendChild( editRelations   );
                pageTop.appendChild( transcription );
                pageTop.appendChild( deletePage );
                li.appendChild( pageTop );
                li.appendChild( img );
                line.grid.appendChild( li );

		visibleImages.push(img);
            }
        }
    
    
        for( var i = 0 ; i < visibleImages.length ; i += 1 ) 
        {
            if( visibleImages[ i ].complete ) 
            {
                imageLoaded( );
            } 
            else 
            {
                visibleImages[ i ].onload = imageLoaded;
            }
        }
    }
}) (DESANTI);
